'use strict';

const conn = require('../../libs/Conn');
const _ = require('underscore');

module.exports = async function (params) {
    let engineeringSql = ``;
    let nodeSql = ``;
    let supervisorSql = ``;
    let progressStatusSql = ``;
    let dateSql = ``;
    let userSql = ``;
    /*工程筛选*/
    if(params.engineering_filter){
        if(params.engineering_filter!=='0') {
            engineeringSql = `AND epid.engineering_id = '${params.engineering_filter}' `;
        }
    }
    /*验收节点筛选*/
    if(params.node_filter){
        if(params.node_filter!=='0'){
            if(params.node_filter === '4'){
                nodeSql = `AND epid.progress_type in (4,5,6) `;
            }else if(params.node_filter === '11'){
                nodeSql = `AND epid.progress_type in (11,12,13,14,15,16,17,18) `;
            }else{
                nodeSql = `AND epid.progress_type = ${params.node_filter} `;
            }
        }


    }
    /*监督人员筛选*/
    if(params.supervisor_filter){
        if(params.supervisor_filter !=='0'){
            supervisorSql = `AND epid.information_id in (SELECT information_id FROM progress_information_user_middle WHERE user_id = '${params.supervisor_filter}')  `;
         }
    }

    /*工程状态筛选*/
    if(params.progressStatus_filter){
        progressStatusSql = `AND ef.status = ${params.progressStatus_filter} `;
    }
    /*时间筛选*/
    if(params.startDateA && params.startDateB){
        dateSql = `AND epid.inspection_date BETWEEN '${params.startDateA}' AND '${params.startDateB}' `;
    }
    /*分页*/
    let pageASql = `LIMIT ${params.pageNum>1?((params.pageNum-1)*10):0},10;SELECT FOUND_ROWS() AS count`;
    let pageBSql = `LIMIT ${params.pageNum>1?((params.pageNum-1)*10):0},10`;

    /*角色判断*/
    if(params.is_manager !==1){
        userSql = `AND epid.engineering_id IN (      SELECT
                                                                engineering_id
                                                            FROM
                                                                engineering_file
                                                            WHERE
                                                                principal_id = '${params.user_id}'
                                                            AND delete_sign = 1
                                                            UNION
                                                                SELECT
                                                                    efa.engineering_id
                                                                FROM
                                                                    engineering_user_middle eum
                                                                INNER JOIN engineering_file efa ON efa.engineering_id = eum.engineering_id
                                                                AND efa.delete_sign = 1
                                                                WHERE
                                                                    eum.user_id = '${params.user_id}' 
                                                            UNION         
                                                                    SELECT
                                                                        epid.engineering_id
                                                                    FROM
                                                                        progress_information_user_middle pium
                                                                    INNER JOIN engineering_progress_information_data epid ON epid.information_id = pium.information_id AND epid.delete_sign = 1
                                                                WHERE  pium.user_id = '${params.user_id}'
                                                            
                                                            ) `;
    }

                let querySql = `SELECT
                                    SQL_CALC_FOUND_ROWS
                                    epid.engineering_id,
                                    ef.engineering_no,
                                    ef.engineering_name,
                                    epid.information_id,
                                    epid.reporter_name AS reporter_name,
                                    epid.reporter_phone AS mobile,
                                    DATE_FORMAT(
                                        epid.inspection_date,
                                        '%Y-%m-%d'
                                    ) AS inspection_date,
                                    epid.inspection_result,
                                    epid.progress_type,
                                    epid.rectification_notice,
                                    epid.rectification_reply,
                                    DATE_FORMAT(
                                        epid.inspection_date,
                                        '%Y-%m-%d'
                                    ) AS inspection_date,
                                    epid.inspection_result,
                                    epid.progress_type,
                                    epid.rectification_notice,
                                    epid.rectification_reply,
                                     DATE_FORMAT(
                                        epid.rectification_date,
                                        '%Y-%m-%d'
                                    ) AS rectification_date,
                                    DATE_FORMAT(
                                        epid.supervision_date,
                                        '%Y-%m-%d'
                                    ) AS supervision_date,
                                    epid.supervision_details,
                                    epid.progress_status,
                                    epid.remarks
                                FROM
                                    engineering_progress_information_data epid
                                LEFT JOIN engineering_file ef ON ef.engineering_id = epid.engineering_id
                                AND ef.delete_sign = 1
                                WHERE
                                    epid.delete_sign = 1 ${userSql} ${engineeringSql} ${nodeSql} ${supervisorSql} ${progressStatusSql} ${dateSql} ${pageASql} ;
                        SELECT
                            pium.information_id,
                            uf.user_id,
                            uf.user_name
                        FROM
                            progress_information_user_middle pium
                        INNER JOIN user_file uf ON uf.user_id = pium.user_id
                        WHERE
                            information_id IN (
                                SELECT
                                    *
                                FROM
                                    (
                                        SELECT
                                            information_id
                                        FROM
                                            engineering_progress_information_data epid
                                        LEFT JOIN  engineering_file ef ON ef.engineering_id = epid.engineering_id
                                        WHERE
                                            epid.delete_sign = 1 
                                           ${userSql} ${engineeringSql} ${nodeSql} ${supervisorSql} ${progressStatusSql} ${dateSql}
                                        ${pageBSql}
                                    ) AS ifid
                            ) ;`;

    let rec = await conn(querySql);
    let informationData = rec[0]; //基础数据
    let userData = rec[2];  // 监督人员数据
    userData = _.groupBy(userData,'information_id');
    let pageNum = Math.ceil(rec[1][0].count/10);
    for(let data in informationData){
        /*报备人员信息*/
        if(informationData[data].mobile){
            informationData[data].reporter_information = informationData[data].reporter_name +'-'+informationData[data].mobile;
        }else{
            informationData[data].reporter_information = informationData[data].reporter_name +'-未登记手机号';
        }

        /*监督人员*/
        let userName = '';
        let userList = [];
        let informationId = informationData[data].information_id;
        if(userData[informationId]){
            for(let user in userData[informationId]){
                userName +=userData[informationId][user].user_name+',';
                userList.push(userData[informationId][user].user_id)
            }
        }
        userName=userName.slice(0,userName.length-1);
        informationData[data].supervisor_name = userName;
        informationData[data].supervisor_id = userList;


        /*验收节点、子节点*/
        if(informationData[data].progress_type === 4 || informationData[data].progress_type === 5 || informationData[data].progress_type === 6 ){
            informationData[data].parent_name = '重要分部验收';
            informationData[data].progress_name = 'important_part_acceptance';
            switch(informationData[data].progress_type){
                case 4:
                    informationData[data].child_name = '地基验槽';
                    break;
                case 5:
                    informationData[data].child_name = '基础分部';
                    break;
                case 6:
                    informationData[data].child_name = '主体分部';
                    break;
            }
        }else if(informationData[data].progress_type === 11 || informationData[data].progress_type === 12 || informationData[data].progress_type === 13 || informationData[data].progress_type === 14 || informationData[data].progress_type === 15 || informationData[data].progress_type === 16 || informationData[data].progress_type === 17 || informationData[data].progress_type === 18){
            informationData[data].parent_name = '工程质量抽测';
            informationData[data].progress_name = 'quality_sampling';
            switch(informationData[data].progress_type){
                case 11:
                    informationData[data].child_name = '结构实体混凝土强度-基础';
                    break;
                case 12:
                    informationData[data].child_name = '结构实体混凝土强度-主体';
                    break;
                case 13:
                    informationData[data].child_name = '结构实体混凝土强度-其他';
                    break;
                case 14:
                    informationData[data].child_name = '钢筋位置、数量、保护层厚度-基础';
                    break;
                case 15:
                    informationData[data].child_name = '钢筋位置、数量、保护层厚度-主体';
                    break;
                case 16:
                    informationData[data].child_name = '钢筋位置、数量、保护层厚度-其他';
                    break;
                case 17:
                    informationData[data].child_name = '切体砂浆强度';
                    break;
                case 18:
                    informationData[data].child_name = '植筋锚固力';
                    break;
            }
        }else{
            informationData[data].child_name = '--';
            switch ( informationData[data].progress_type) {
                case 1:
                    informationData[data].progress_name = 'foundation_test_tanks';
                    informationData[data].parent_name = '地基验槽';
                    break;
                case 2:
                    informationData[data].progress_name = 'material_check_a';
                    informationData[data].parent_name = '建筑材料/构配件抽查(第一次)';
                    break;
                case 3:
                    informationData[data].progress_name = 'foundation_test';
                    informationData[data].parent_name = '基础验筋';
                    break;
                case 7:
                    informationData[data].progress_name = 'first_conversion_reinforcement';
                    informationData[data].parent_name = '首层或转换层验筋';
                    break;
                case 8:
                    informationData[data].progress_name = 'material_check_b';
                    informationData[data].parent_name = '建筑材料/构配件抽查(第二次)';
                    break;
                case 9:
                    informationData[data].progress_name = 'standard_reinforcement';
                    informationData[data].parent_name = '标准层钢筋';
                    break;
                case 10:
                    informationData[data].progress_name = 'top_reinforcement';
                    informationData[data].parent_name = '顶层钢筋';
                    break;
                case 19:
                    informationData[data].progress_name = 'concrete_structure';
                    informationData[data].parent_name = '主体砼结构';
                    break;
                case 20:
                    informationData[data].progress_name = 'material_check_c';
                    informationData[data].parent_name = '建筑材料/构配件抽查(第三次)';
                    break;
                case 21:
                    informationData[data].progress_name = 'main_structure';
                    informationData[data].parent_name = '主体结构';
                    break;
                case 22:
                    informationData[data].progress_name = 'other_projects';
                    informationData[data].parent_name = '装饰装修等工程';
                    break;
            }
        }

        /*状态*/
                switch (informationData[data].progress_status) {
                    case 1:
                        informationData[data].progress_status = '未进行';
                        break;
                    case 2:
                        informationData[data].progress_status = '进行中';
                        break;
                    case 3:
                        informationData[data].progress_status = '完成';
                        break;
                }

    }

    function keysort(key,sortType){
        return function(a,b){
            return sortType ?(a[key]<b[key]):(a[key]>b[key])
        }
    }
    /*排序*/
    if(params.sort&&params.dir){
        switch (params.sort) {
            case 'engineering_no':
                switch (params.dir) {
                    case 'ASC':
                        informationData.sort(keysort('engineering_no',true));
                        break;
                    case 'DESC':
                        informationData.sort(keysort('engineering_no',false));
                        break;
                }
                break;
            case 'inspection_date':
                switch (params.dir) {
                    case 'ASC':
                        informationData.sort(keysort('inspection_date',true));
                        break;
                    case 'DESC':
                        informationData.sort(keysort('inspection_date',false));
                        break;
                }
                break;
            case 'parent_name':
                switch (params.dir) {
                    case 'ASC':
                        informationData.sort(keysort('parent_name',true));
                        break;
                    case 'DESC':
                        informationData.sort(keysort('parent_name',false));
                        break;
                }
                break;
            case 'child_name':
                switch (params.dir) {
                    case 'ASC':
                        informationData.sort(keysort('child_name',true));
                        break;
                    case 'DESC':
                        informationData.sort(keysort('child_name',false));
                        break;
                }
                break;
            case 'reporter_information':
                switch (params.dir) {
                    case 'ASC':
                        informationData.sort(keysort('reporter_information',true));
                        break;
                    case 'DESC':
                        informationData.sort(keysort('reporter_information',false));
                        break;
                }
                break;
            case 'supervisor_name':
                switch (params.dir) {
                    case 'ASC':
                        informationData.sort(keysort('supervisor_name',true));
                        break;
                    case 'DESC':
                        informationData.sort(keysort('supervisor_name',false));
                        break;
                }
                break;
            case 'progress_status':
                switch (params.dir) {
                    case 'ASC':
                        informationData.sort(keysort('progress_status',true));
                        break;
                    case 'DESC':
                        informationData.sort(keysort('progress_status',false));
                        break;
                }
                break;
        }
    }
    if(informationData.length>0){
        informationData[0].dataTotal = rec[1][0].count;
        informationData[0].pageTotal = pageNum;
        informationData[0].pageNum = params.pageNum;
    }
    return {success: true, data: informationData};

};